import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:xvideos/hj_page/community_business/widget/community_post_widget.dart';
import 'package:xvideos/hj_page/user_center_page/view/user_work_list_logic.dart';
import 'package:xvideos/tools_base/loading/l_error_widget.dart';
import 'package:xvideos/tools_base/loading/loading_center_widget.dart';
import 'package:xvideos/tools_base/refresh/pull_refresh.dart';

import '../../../routers/jump_router.dart';
import '../../cartoon_business/search_tuji_cell.dart';
import '../../home_business/home_cell_style/video_simple_cell_widget.dart';
import '../../home_business/search_page/search_result_page/search_video_result_page.dart';

class UserWorkListView extends StatelessWidget {
  final int uid;
  final SearchType newsType;

  const UserWorkListView(
    this.uid,
    this.newsType, {
    super.key,
  });

  @override
  Widget build(BuildContext context) {
    return GetBuilder(
      init: UserWorkListLogic(uid, newsType),
      tag: "${DateTime.now().millisecondsSinceEpoch}",
      builder: (logic) {
        if (logic.isLoading) return LoadingCenterWidget();
        if (logic.dataSource.isEmpty)
          return CErrorWidget(
            retryOnTap: () {
              logic.fetchPageData(needLoading: true);
            },
          );
        return pullYsRefresh(
          onInit: (refr) => logic.refreshController = refr,
          onRefresh: (refr) => logic.fetchPageData(),
          onLoading: (refr) => logic.fetchPageData(isRefresh: false),
          child: _buildTypeList(logic),
        );
      },
    );
  }

  Widget _buildTypeList(UserWorkListLogic logic) {
    if (newsType == SearchType.video || newsType == SearchType.short) {
      bool isShort = newsType == SearchType.short;
      return GridView.builder(
        padding: EdgeInsets.only(left: 12, right: 12),
        gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
          crossAxisCount: 2,
          mainAxisSpacing: 12,
          crossAxisSpacing: 6,
          childAspectRatio: isShort ? 168 / 246 : 168 / 154,
        ),
        itemCount: logic.dataSource.length,
        itemBuilder: (context, index) {
          return VideoSimpleCellWidget(
            videoModel: logic.dataSource[index],
            textLines: isShort ? 1 : 2,
            isShowBottom: isShort ? false : true,
            onTap: () {
              pushToVideoPage(videoModel: logic.dataSource[index], videoArr: logic.dataSource);
            },
          );
        },
      );
    } else if (newsType == SearchType.image) {
      return GridView.builder(
        padding: EdgeInsets.only(left: 12, right: 12),
        gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
          crossAxisCount: 2,
          mainAxisSpacing: 12,
          crossAxisSpacing: 6,
          childAspectRatio: 168 / 246,
        ),
        itemCount: logic.dataSource.length,
        itemBuilder: (context, index) {
          return SearchTujiCell(
            videoModel: logic.dataSource[index],
            textline: 1,
          );
        },
      );
    } else {
      return ListView.builder(
        padding: EdgeInsets.zero,
        itemCount: logic.dataSource.length,
        itemBuilder: (context, index) {
          return CommunityPostWidget(
            videoModel: logic.dataSource[index],
            videoModels: logic.dataSource,
          );
        },
      );
    }
  }
}
